package com.anonymous3;

import org.junit.Test;

//使用匿名内部类对象来实现线程的创建与启动
public class Anonymous {
    @Test
    public void test1() {
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    System.out.println(getName() + ":正在执行" + i);

                }
            }
        }.start();
    }

    @Test
    public void test2() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 3; i++) {
                    System.out.println(Thread.
                            currentThread().getName() + ":" + i);
                }

            }
        }).start();
    }

    @Test
    public void priority() {
        Thread thread = new Thread() {
            @Override
            public void run() {
                System.out.println(getName() + "的优先级" + getPriority());
            }
        };
//        setPriority()设置线程优先级
        thread.setPriority(Thread.MAX_PRIORITY);
        thread.start();

        Thread thread1 = new Thread() {
            @Override
            public void run() {
                System.out.println(getName() + "的优先级" + getPriority());
            }
        };
//        setPriority()设置线程优先级
        thread1.setPriority(Thread.MIN_PRIORITY);

        thread1.start();
//        返回对当前正在执行的线程对象
        System.out.println(Thread.currentThread().getName() + "的优先级："
                + Thread.currentThread().getPriority());

    }


}
